// 模拟文件IO操作的工具类
// 由于Previewer环境无法访问真实文件系统，使用内存对象模拟文件存储

export class FileIOMock {
  private static fileCache: Map<string, string> = new Map<string, string>();
  private static initialized: boolean = false;
  
  // 初始化模拟数据
  private static initializeData(): void {
    if (!this.initialized) {
      // 初始化案件数据
      this.fileCache.set('cases.json', JSON.stringify([
        {
          id: '1',
          title: '珠宝店盗窃案',
          description: '市中心珠宝店发生盗窃案，价值数百万的珠宝被盗',
          status: 'OPEN',
          createTime: '2025-10-15T08:30:00',
          updateTime: '2025-10-15T08:30:00',
          createBy: '1'
        },
        {
          id: '2',
          title: '可疑人物跟踪',
          description: '有人举报在住宅区发现可疑人物出没',
          status: 'IN_PROGRESS',
          createTime: '2025-10-16T14:20:00',
          updateTime: '2025-10-17T09:15:00',
          createBy: '2'
        },
        {
          id: '3',
          title: '车辆失窃案',
          description: '高档轿车在停车场被盗',
          status: 'CLOSED',
          createTime: '2025-10-10T11:45:00',
          updateTime: '2025-10-14T16:30:00',
          createBy: '1'
        }
      ]));
      
      // 初始化用户数据
      this.fileCache.set('users.json', JSON.stringify([
        {
          id: '1',
          username: 'zhang_sir',
          realName: '张探长',
          role: 'DETECTIVE',
          email: 'zhang@police.com',
          phone: '13800138001',
          createTime: '2025-10-01T00:00:00'
        },
        {
          id: '2',
          username: 'li_officer',
          realName: '李警官',
          role: 'OFFICER',
          email: 'li@police.com',
          phone: '13900139002',
          createTime: '2025-10-02T00:00:00'
        }
      ]));
      
      // 初始化嫌疑人数据
      this.fileCache.set('suspects.json', JSON.stringify([
        {
          id: '1',
          caseId: '1',
          name: '王某',
          age: 35,
          gender: 'MALE',
          occupation: '无业',
          suspicionLevel: 'HIGH',
          description: '案发时间在现场附近出现',
          lastSeenLocation: '珠宝店后门',
          lastSeenTime: '2025-10-15T08:15:00'
        },
        {
          id: '2',
          caseId: '2',
          name: '李某',
          age: 42,
          gender: 'MALE',
          occupation: '保安',
          suspicionLevel: 'MEDIUM',
          description: '行为举止异常',
          lastSeenLocation: '住宅区附近',
          lastSeenTime: '2025-10-16T14:00:00'
        },
        {
          id: '3',
          caseId: '3',
          name: '赵某',
          age: 28,
          gender: 'FEMALE',
          occupation: '服务员',
          suspicionLevel: 'LOW',
          description: '曾在停车场停留',
          lastSeenLocation: '停车场',
          lastSeenTime: '2025-10-10T11:30:00'
        }
      ]));
      
      this.initialized = true;
    }
  }
  
  /**
   * 读取JSON文件
   * @param filePath 文件路径
   * @returns 文件内容对象
   */
  public static async readJSONFile(filePath: string): Promise<any> {
    this.initializeData();
    
    // 提取文件名作为缓存键
    const fileName = filePath.split('/').pop() || filePath.split('\\').pop() || filePath;
    
    if (this.fileCache.has(fileName)) {
      return JSON.parse(this.fileCache.get(fileName) || '{}');
    }
    
    // 返回空数组作为默认值
    return [];
  }
  
  /**
   * 写入JSON文件
   * @param filePath 文件路径
   * @param data 要写入的数据
   * @returns 是否写入成功
   */
  public static async writeJSONFile(filePath: string, data: any): Promise<boolean> {
    this.initializeData();
    
    // 提取文件名作为缓存键
    const fileName = filePath.split('/').pop() || filePath.split('\\').pop() || filePath;
    
    try {
      this.fileCache.set(fileName, JSON.stringify(data));
      return true;
    } catch (error) {
      console.error('Failed to write JSON file:', error);
      return false;
    }
  }
  
  /**
   * 获取模拟数据
   * @param fileName 文件名
   * @returns 模拟数据
   */
  public static getMockData(fileName: string): any {
    this.initializeData();
    
    if (this.fileCache.has(fileName)) {
      return JSON.parse(this.fileCache.get(fileName) || '{}');
    }
    
    return [];
  }
  
  /**
   * 清除缓存（用于测试）
   */
  public static clearCache(): void {
    this.fileCache.clear();
    this.initialized = false;
  }
}